#if 0

#include <iostream>
#include <functional>
#include <thread>
#include <chrono>
#include <unistd.h>
void async_operation(std::function<void(int)> callback) {
    // 模拟一些计算
    std::this_thread::sleep_for(std::chrono::seconds(1));
    int result = 42;
    // 调用回调函数并将结果传递给它
    callback(result);
}
void callback_function(int result) {
    std::cout << "Async operation completed with result: " << result << std::endl;
}
int main() {
    // 发起异步操作，并在操作完成后调用回调函数
    async_operation(callback_function);
    // 主线程继续执行其他任务
    while (1)
    {
       std::cout << "Main thread continues to do other work..." << std::endl;
       sleep(1);
    }
    
    
    return 0;
}

#endif

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
using namespace std;

//--------------
// 类中定义线程，并实现回调
// A应用
template <typename Tobject, typename Tparam>
class AsynA
{
    public:
        struct ThreadParam
        {
            AsynA *pthis;
            Tparam a;
        }; // 根据线程参数自定义结构
        typedef void (Tobject::*Cbfun)(Tparam);

    public:
        AsynA() : number(1)
        {
            printf("AsynA : AsynA()\n");
        }
        void print(void)
        {
            printf("print : number = %d \n", number);
        }
        static void *funCallbackThread(void *param); 
        void CreateCallbackThread(Tobject *pInstance, Cbfun pFun, Tparam a);
        
        void ThreadFunc(Tparam a);                   

    private:
        int number;
        Cbfun pCbfun;
        Tobject *m_pInstance;
};

template <typename Tobject, typename Tparam>
void AsynA<Tobject, Tparam>::CreateCallbackThread(Tobject *pInstance, Cbfun pFun, Tparam a)
{
    ThreadParam *p = new ThreadParam;
    p->pthis = this;
    p->a = a;
    m_pInstance = pInstance;
    pCbfun = pFun;

    pthread_t thing1;
    pthread_create(&thing1, NULL, &funCallbackThread, (void *)p);
}

template <typename Tobject, typename Tparam>
void *AsynA<Tobject, Tparam>::funCallbackThread(void *param)
{
    ThreadParam *p = (ThreadParam *)param;
    printf("ThreadParam p->a = %d\n", p->a);
    p->pthis->ThreadFunc(p->a);
    return 0;
}
template <typename Tobject, typename Tparam>
void AsynA<Tobject, Tparam>::ThreadFunc(Tparam a)
{
    printf("%d ThreadFunc : number = %d \n",number);
    while (1)
    {
        const pthread_t threadID = pthread_self();
        sleep(a);
        (m_pInstance->*pCbfun)((int)threadID);
    }
}
// B应用
class AsynB
{
    public:
        AsynB() : number(0)
        {
        }
        void fCallBack(int p)
        {
            printf("Thread ID = %ld\n", p);
        }
    private:
        int number;
};

int main(void)
{
    // 类中定义线程，并实现回调
    AsynA<AsynB, int> asyn_a;
    AsynB asyn_b;
    int p = 2;
    asyn_a.CreateCallbackThread(&asyn_b, &AsynB::fCallBack, p);

    AsynA<AsynB, int> asyn_a1;
    AsynB asyn_b1;
    int p1 = 4;
    asyn_a1.CreateCallbackThread(&asyn_b1, &AsynB::fCallBack, p1);
    
    // 主程序阻塞在这里
    getchar();
    return 0;
}
